﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AAEngine.Engine;

namespace AAEngine.Rendering2D.Renderers
{
    public class ImageRenderer : RendererBase
    {
        public Texture2D ImageData { get; set; }

        public override void Render(Box worldExtents, RenderQueue renderQueue)
        {
            if (ImageData == null)
            {
                return;
            }

            float tmpScaleX = _scale.X;
            float tmpScaleY = _scale.Y;
            if(_size != null)
            {
                tmpScaleX = _scale.X * (_size.Value.X / ImageData.Width);
                tmpScaleY = _scale.Y * (_size.Value.Y / ImageData.Height);
            }

            RenderInstance drawObject = new RenderInstance();
            drawObject.Texture = ImageData;
            drawObject.SourceRectangle = new Rectangle(0, 0, ImageData.Width, ImageData.Height);
            drawObject.Position = _position + _positionOffset; // new Point(_spatialComponent.Position.X - Size.X / 2, _spatialComponent.Position.Y - Size.Y / 2);
            //drawObject.Size = new Point((int)_size.X, (int)_size.Y);
            drawObject.Scale = new Vector2(tmpScaleX, tmpScaleY);
            drawObject.Rotation = _rotation + _rotationOffset;
            drawObject.Alpha = _alpha;
            drawObject.Center = _registrationPoint;

            renderQueue.AddRenderInstance(drawObject);
        }

        public override Box WorldExtents
        {
            get
            {
                if (ImageData == null)
                {
                    return new Box();
                }

                float tmpScaleX = _scale.X;
                float tmpScaleY = _scale.Y;
                if (_size != null)
                {
                    tmpScaleX = _scale.X * (_size.Value.X / ImageData.Width);
                    tmpScaleY = _scale.Y * (_size.Value.Y / ImageData.Height);
                }

                float width = ImageData.Width * 0.5f * Math.Abs(tmpScaleX);
                float height = ImageData.Height * 0.5f * Math.Abs(tmpScaleY);

                return new Box(Position.X - width * 0.5f, Position.Y - height * 0.5f, width, height);
            }
        }
    }
}
